package jlisp.tools;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public class SettingTools {
	private static List<Control> concealControlList = new ArrayList<Control>();//处于隐藏的组件的集合
	private static List<Control> parentControlList = new ArrayList<Control>();//处于隐藏的组件的父面板的集合
	public static void setControlCentre(Control control) {//设置组件居中
		if("org.eclipse.swt.widgets.Shell".equals(control.getClass().getName())) {
			Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
			Point p = control.getSize();
			control.setLocation((screen.width-p.x)/2,(screen.height-p.y)/2);
		}else {
			Composite composite = control.getParent();
			Point pCom = composite.getSize();
			Point pCon = control.getSize();
			control.setLocation((pCom.x-pCon.x)/2,(pCom.y-pCon.y)/2);
		}
	}
	public static void setBackImageSelfAdapt(Composite composite) {//设置图片自适应面板
		Image image = composite.getBackgroundImage();
		int imagewidth = image.getBounds().width;
		int imageheight = image.getBounds().height;
		composite.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				// TODO Auto-generated method stub
				Point pCom = composite.getSize();
				e.gc.drawImage(image, 0, 0, imagewidth, imageheight, 0, 0, 1080, 480);
				//e.gc.drawImage(image, 0, 0, imagewidth, imageheight, 0, 0, pCom.x, pCom.y);
			}
			
		});
	}
	public static void setControlSelfAdapt(Control control) {//设置组件自适应容器
		Rectangle rtCon = control.getBounds();
		
		Rectangle rtPa = control.getParent().getBounds();
		double[] proportion = new double[4];
		proportion[0] = (double)rtCon.x/rtPa.width;
		proportion[1] = (double)rtCon.y/rtPa.height;
		proportion[2] = (double)rtCon.width/rtPa.width;
		proportion[3] = (double)rtCon.height/rtPa.height;
		control.getParent().addControlListener(new ControlAdapter() {


			@Override
			public void controlResized(ControlEvent arg0) {
				// TODO Auto-generated method stub
				Rectangle tempR = control.getParent().getBounds();
				control.setBounds(new Rectangle((int)(tempR.width*proportion[0]), (int)(tempR.height*proportion[1]), 
						(int)(tempR.width*proportion[2]), (int)(tempR.height*proportion[3])));


				
			}
			
		});
	}
	public static void setBackTransparent(Control control) {
		control.getParent().setBackgroundMode(SWT.INHERIT_DEFAULT);
	}
	//???
	public static void setVisibleFalse(Control control) {//将组件隐藏(原理：设置位置到父面板之外)
		control.setLocation(control.getParent().getSize().x,0);
		Control parent = control.getParent();
		boolean haveSameParent = false;
		for(int i = 0;i < parentControlList.size();i++) {
			if(parent == parentControlList.get(i)) {
				haveSameParent = true;
				break;
			}
		}
		if(!haveSameParent) {//如果没有相同的父面板
			parentControlList.add(parent);
			concealControlList.add(control);
			parent.addControlListener(new ControlAdapter() {
				public void controlResized(ControlEvent e) {
					for(int i = 0;i < concealControlList.size();i++) {
						if(concealControlList.get(i).getParent() == parent) {
							concealControlList.get(i).setLocation(parent.getSize().x,0);
						}
					}
				}
			});

		}else {//如果有相同的父面板
			boolean haveSameControl = false;
			for(int i = 0;i < concealControlList.size();i++) {
				if(control == concealControlList.get(i)) {
					haveSameControl = true;
					return;
				}
			}
			if(!haveSameControl) {
				concealControlList.add(control);
			}
		}
	}
	public static void setVisibleTrue(Control control) {
		for(int i = 0;i < concealControlList.size();i++) {
			if(concealControlList.get(i) == control) {
				concealControlList.remove(i--);
			}
		}
	}
}
